<?php
// app/Controller/UsersController.php
App::uses('AppController', 'Controller');

class UsersController extends AppController {
	
public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('register','login', 'logout');
}

    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    public function register() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'login'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
    }

    public function edit($id = null) {
    	$this->User->id = $id;
    	if ($this->request->is('post')) {
    		if ($this->User->save($this->request->data)) {
    			$this->Session->setFlash(__('The changes has been saved'));
    			return $this->redirect(array('controller' => 'start', 'action' => 'index'));
    		}
    		$this->Session->setFlash(
    				__('The changes could not be saved. Please, try again.')
    		);
    	}
    }

    public function login() {
    	if ($this->request->is('post')) {
    		if ($this->Auth->login()) {
    			return $this->redirect($this->Auth->redirectUrl());
    		}
    		$this->Session->setFlash(__('Invalid username or password, try again'));
    	}
    }
    
    public function logout() {
    	return $this->redirect($this->Auth->logout());
    }
 
/*   public function view($id = null) {
     $this->User->id = $id;
     if (!$this->User->exists()) {
     throw new NotFoundException(__('Invalid user'));
     }
     $this->set('user', $this->User->read(null, $id));
     }*/

/*    public function edit($id = null) {
    if ($this->request->is('post')) {
            $this->User->edit();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The Changes has been saved'));
                return $this->redirect(array('controller' => 'start', 'action' => 'main'));
            }
            $this->Session->setFlash(
                __('TheChanges could not be saved. Please, try again.')
            );
        }
    }
*/    

 /*   public function delete($id = null) {
        // Prior to 2.5 use
        // $this->request->onlyAllow('post');

        $this->request->allowMethod('post');

        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        return $this->redirect(array('action' => 'index'));
    }*/

}
?>